﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Exception = System.Exception;

namespace GetContent 
{
    class DataFilter
    {
        protected  CookieContainer Cc = new CookieContainer();
        protected string _lastError;
        public DataGridViewCell Status;
        public int _delay;
        public string _singleLink;
        public string _filter;
        //public static DataGridView holder;
        public  string GetReg(string pattern, string source)
        {
            var r = new Regex(pattern);
            return r.Match(source).Value;
        }

        public  MatchCollection GetMatches(string pattern, string source)
        {
            var r = new Regex(pattern);
            return r.Matches(source);
        }

        //Gửi Request và nhận dữ liệu trả về
        public  object GetData(string url, string referer = null, string _params = null, bool returnByte = false)
        {
            try
            {
                

                var rq = (HttpWebRequest)WebRequest.Create(url);
                //if (PR != null) rq.Proxy = PR;

                rq.UserAgent = "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.99 CoRom/28.0.1500.99 Safari/537.36";
                rq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                if (referer != null) rq.Referer = referer;
                rq.Headers["Accept-Language"] = "en-US,en;q=0.8";
                rq.Headers["Accept-Encoding"] = "gzip,deflate,sdch";
                //rq.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.3";

                rq.Timeout = 30000;
                rq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

                rq.CookieContainer = Cc;
                if (_params != null)
                {
                    rq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                    rq.Method = "POST";
                    var bytes = Encoding.UTF8.GetBytes(_params);
                    rq.ContentLength = bytes.Length;

                    using (var os = rq.GetRequestStream())
                    {
                        os.Write(bytes, 0, bytes.Length);
                    }
                }
                var rp = (HttpWebResponse)rq.GetResponse();

                var stream = rp.GetResponseStream();
                object data;
                if (returnByte)
                {
                    var reader = new BinaryReader(stream);
                    data = reader.ReadBytes(1 * 1024 * 1024 * 10);
                }
                else
                {
                    var read = new StreamReader(stream);
                    data = read.ReadToEnd();
                    read.Close();
                }
                rp.Close();
                return data;
            }
            catch (Exception ex)
            {
                return null;
            }
        }



        /*
         * @ CheckMatch(List,content)
         * Return 
         * +> False if word not in content
         * +> True if on of  words in content
         */
        public bool CheckMatch(List<string> list , string content )
        {
            var rs = false;
            if (list.Count < 0) return rs;
            var check = 0 ;
            content = content.ToUpper();

            foreach (var s in list)
            {
                var s1 = s.ToUpper().Trim();
                
                if (content.Contains(s1))
                {
                    check++;
                }
            }
            if (check > 0) rs = true;
            return rs;
        }

        public string GetPhone(string content)
        {
            content = content.Replace(".", "").Replace(" ", "").Replace("-", "");

            var phone = "";
            var patterm = @"\d{8,11}";
            var myregex = new Regex(patterm);
            phone = myregex.Match(content).Value;
            /*string p2 = @"\d{3}.\d{3}.\d{4}";
            if (phone == "")
            {
                myregex = new Regex(p2);
                phone = myregex.Match(content).Value;
            }*/
           
            return phone;
        }

        public string GetDate(string content)
        {
            var date = "";
            var patterm = @"\d{2}-\d{2}-\d{2}";
            var myregex = new Regex(patterm);
            date = myregex.Match(content).Value;
            return date;
        }


        protected void Delay(int time)
        {
            for (var i = time; i >= 0; i--)
            {
                Status.Value = string.Format("Delay {0}", i);
                Thread.Sleep(1000);
            }
        }

        protected bool CheckDate(System.DateTime itemDate)
        {
                var cDate = false;
                var toDate = Configs.ReadValue("toDate");
                var toDate1 = System.DateTime.Parse(toDate);

                var fromDate = Configs.ReadValue("fromDate");
                var formDate1 = System.DateTime.Parse(fromDate);

                if (itemDate >= formDate1 && itemDate <= toDate1)
                {
                    cDate = true;
                }
                
                return cDate;
        }



    }
}
